import 'package:flutter/material.dart';
import 'package:labcloud_app/datas/user.dart';

import '../api/uaa/uaa_service.dart';

class UserModel extends ChangeNotifier {
  User? _user;

  User? get user => _user;
  bool get isLoggedIn => _user != null;

  Future<void> login({required String username, required String password}) async {
      var response = await UAAService.login(username: username, password: password);
      _user = User.fromJson(response.data);
      notifyListeners();
  }

  Future<void> emailCode({required String email}) async {
    await UAAService.getEmailCode(email: email);
  }

  Future<void> loginByEmail({required String email, required String code}) async {
    var response = await UAAService.loginByEmail(email: email, code: code);
    _user = User.fromJson(response.data);
    notifyListeners();
  }

  // 清除用户信息（退出登录）
  void clearUser() {
    _user = null;
    notifyListeners();
  }
}
